<template>
  <!-- 维护作业- Magnum日志权限页面 -->
  <div class="fillcontain">
    <el-form :model="searchParams" :rules="rules" ref="ruleForm" label-width="100px" class="search_container">
      <el-row>
        <el-col :span="6">
          <el-form-item label="使用者代码" prop="userId">
            <el-input clearable v-model="searchParams.userId" placeholder="请输入使用者代码"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="日志权限" prop="logPermission">
            <el-select v-model="searchParams.logPermission" clearable placeholder="请选择">
              <el-option
                  v-for="item in logPermissions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6" class="text_center">
          <el-button type="primary" icon="el-icon-search" @click="subGetList"></el-button>
          <el-button type="primary" plain icon="el-icon-refresh" @click="resetSearch"></el-button>
        </el-col>
      </el-row>
    </el-form>
    <div class="contain">
      <!-- 查询结果 -->
      <div class="table_container">
        <el-table
            v-loading="loading"
            :data="canceltableData"
            highlight-current-row
            header-cell-class-name="table-header-class"
            style="width:100%">
          <el-table-column
              min-width="120"
              property="userid"
              label="使用者代码"
              align='left'
              show-overflow-tooltip>
          </el-table-column>
          <el-table-column
              min-width="80"
              property="procflg04"
              label="日志权限"
              align='left'
              show-overflow-tooltip>
            <template slot-scope="scope">
              <el-select v-model="scope.row.procflg04"
                         @visible-change="updateProcflg($event, scope.row)"
                         placeholder="">
                <el-option
                    v-for="item in options"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                </el-option>
              </el-select>
            </template>
          </el-table-column>
        </el-table>
        <el-row>
          <el-col :span="24">
            <div class="pagination">
              <el-pagination
                  background
                  v-if='paginations.total > 0'
                  :page-sizes="paginations.pageSizes"
                  :page-size="paginations.pageSize"
                  :layout="paginations.layout"
                  :total="paginations.total"
                  :current-page='paginations.pageIndex'
                  @current-change='handleCurrentChange'
                  @size-change='handleSizeChange'>
              </el-pagination>
            </div>
          </el-col>
        </el-row>
      </div>
    </div>
  </div>
</template>

<script>
  import {mapState, mapGetters} from 'vuex'
  import constant from '@/utils/constant';
  import {getMagnumlogList, updateMagnumlog} from "@/api/setting/maintain";
  import validRules from "@/utils/validRules";

  export default {
    data() {
      return {
        // 查询条件
        rules: {
          userId: [
            {validator: validRules.isEnglishOrNumber, message: '请正确输入使用者代码！', trigger: 'blur'}
          ],
          logPermission: [],
        },
        searchParams: {
          userId: '',
          logPermission: ''
        },
        canceltableData: [], //数据
        loading: false,
        logPermissions: [
          {value: ' ', label: 'N'},
          {value: 'Y', label: 'Y'}
        ],
        options: [
          {value: '', label: ''},
          {value: 'Y', label: 'Y'}
        ],
        procflg04_temp: '', // 用于检测日志权限是否改变

        //需要给分页组件传的信息
        paginations: {
          total: 0,        // 总数
          pageIndex: 1,  // 当前位于哪页
          pageSize: 10,   // 1页显示多少条
          pageSizes: [5, 10, 15, 20],  //每页显示多少条
          layout: "total, sizes, prev, pager, next, jumper"   // 翻页属性
        }
      }
    },
    created() {
      this.getList();
    },
    mounted() {
      // this.getList();
    },
    methods: {
      // 查询列表
      getList() {
        this.$refs['ruleForm'].validate((valid) => {
          if (valid) {
            this.paginations.total = 0;
            this.tableData = [];
            let para = {
              "userId": this.searchParams.userId,
              "logPermission": this.searchParams.logPermission,
              "pageSize": this.paginations.pageSize,
              "pageNumber": this.paginations.pageIndex,
            }
            // if(!para.customerNo&&!para.customerName&&!para.telephone&&!para.idNo&&!para.sex&&!para.birthday){
            //     this.$message({
            //         message: '请至少输入一个查询条件来查询！',
            //         type: 'error'
            //     });
            //     return false
            // }
            this.$store.dispatch('loading/CHANGE_LOADING', true);
            getMagnumlogList(para).then(res => {
              console.log('magnum日志权限查询数据：', res);
              this.$store.dispatch('loading/CHANGE_LOADING', false);
              if (res.resultCode == constant.SUCCESS) {
                if (res.data && res.data.list.length) {
                  this.paginations.total = res.data.total;

                  // 日志权限去空格
                  res.data.list.forEach(item => {
                    if (item.procflg04) {
                      item.procflg04 = item.procflg04.trim();
                    } else {
                      item.procflg04 = '';
                    }
                  });

                  this.tableData = res.data.list;
                  this.canceltableData = res.data.list;
                } else {
                  this.$message({
                    message: '无符合条件任务！',
                    type: 'error'
                  });
                }
              }
            }).catch(err => {
              this.$store.dispatch('loading/CHANGE_LOADING', false);
            })
          }
        });
      },
      // 打开/关闭 下拉框事件
      updateProcflg(_event, val) {
        const procflg04Temp = val.procflg04.trim();
        if (_event) {
          this.procflg04_temp = procflg04Temp;
        } else {
          if (this.procflg04_temp != procflg04Temp) {
            this.$confirm('确定要修改', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              let param = {
                userId: val.userid.trim(),
                logPermission: val.procflg04.trim()
              };
              updateMagnumlog(param).then(res => {
                //this.$store.dispatch('loading/CHANGE_LOADING',false)
                if (res.resultCode == constant.SUCCESS) {
                  this.$message({
                    type: 'success',
                    message: res.resultMsg
                  });
                } else {
                  // 修改失败--需要回滚数据
                  val.procflg04 = this.procflg04_temp;
                }
              }).catch(err => {
                // this.$store.dispatch('loading/CHANGE_LOADING',false);
                // 修改异常--需要回滚数据
                val.procflg04 = this.procflg04_temp;
              });
            }).catch(() => {
              // 修改取消--需要回滚数据
              val.procflg04 = this.procflg04_temp;
              this.$message({
                type: 'info',
                message: '已取消修改'
              });
            });
          }
        }
      },

      // 每页多少条切换
      handleSizeChange(pageSize) {
        this.paginations.pageSize = pageSize;
        this.getList();
      },
      // 上下分页
      handleCurrentChange(page) {
        this.paginations.pageIndex = page;
        this.getList();
      },
      // 根据条件查询列表
      subGetList() {
        this.paginations.pageIndex = 1;
        this.getList();
      },
      // 清除查询条件
      resetSearch() {
        this.searchParams.userId = '';
        this.searchParams.logPermission = '';
      }
    },
    computed: {
      ...mapGetters(['tags']),
      reload() {
        return this.$store.state.loading.reload;
      }
    },
    activated() {
      if (this.reload) {
        this.$store.dispatch('loading/CHNAGE_RELOAD', false)
        Object.assign(this.$data, this.$options.data());
      }
    }
  }
</script>

<style lang="less" scoped>
  .fillcontain {
    padding-bottom: 0;
  }

  .contain {
    background: #fff;
    padding: 10px;
    margin-bottom: 20px;
  }

  .pagination {
    padding: 10px 20px;
    text-align: right;
  }

  .operationIcon {
    // padding: 12px;
    border: 1px solid #e5e5e5;
    border-radius: 50%;
    width: 22px;
    height: 22px;
    display: inline-block;
    cursor: pointer;
    box-shadow: 0 0 3px #f9f9f9;
  }

  .search_container {
    padding: 20px 20px 0 20px;
    //    border-top: 1px solid #e1e1e1;
  }

  .common-box {
    padding-bottom: 15px;
  }

  .common-lab {
    line-height: normal !important;
    text-align: right;

    .labelName {
      text-align: right;
    }
  }
</style>



